<script setup lang="ts">
import { UserFilled, Close, Edit, Plus } from '@element-plus/icons-vue';
import AddNode from './AddNode.vue';
import UserRoleSelect from '../UserRoleSelect.vue';
import { useOpenSelect, useNodeConfig } from '../hooks';
import { IApproverNodeConfig, EApproverType, EDirectorMode, EUserSelectType, EExamineMode, ETermMode } from '../types';

const { selectRef, openSelect } = useOpenSelect();

const model = defineModel<IApproverNodeConfig>({ default: {} });

const { drawerVisible, isEditTitle, form, nodeConfig, nodeTitleRef, show, editTitle, save, delNode } = useNodeConfig(model);

function toText(nodeConfig: IApproverNodeConfig) {
	if (nodeConfig.setType == EApproverType.USER) {
		if (nodeConfig.nodeUserList && nodeConfig.nodeUserList.length > 0) {
			const users = nodeConfig.nodeUserList.map((item) => item.name).join('、');
			return users;
		} else {
			return false;
		}
	} else if (nodeConfig.setType == EApproverType.DIRECTOR) {
		return '直接主管';
	} else if (nodeConfig.setType == EApproverType.MULTI_LEVEL) {
		return '连续多级主管';
	}
}

function delUser(index: number) {
	form.value.nodeUserList.splice(index, 1);
}
</script>

<template>
	<div class="node-wrap">
		<div class="node-wrap-box" @click="show">
			<div class="title" style="background: #ff943e">
				<el-icon class="icon">
					<UserFilled />
				</el-icon>
				<span>{{ nodeConfig.nodeName }}</span>
				<el-icon class="close" @click.stop="delNode()">
					<Close />
				</el-icon>
			</div>
			<div class="content">
				<span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
				<span v-else class="placeholder">请选择</span>
			</div>
		</div>

		<add-node v-model="nodeConfig.childNode"></add-node>

		<el-drawer title="审批人设置" v-model="drawerVisible" destroy-on-close append-to-body :size="500">
			<template #header>
				<div class="node-wrap-drawer__title">
					<label @click="editTitle" v-if="!isEditTitle"
						>{{ form.nodeName }}<el-icon class="node-wrap-drawer__title-edit"> <Edit /> </el-icon
					></label>
					<el-input
						v-if="isEditTitle"
						ref="nodeTitleRef"
						v-model="form.nodeName"
						clearable
						@blur="isEditTitle = false"
						@keyup.enter="isEditTitle = false"
					></el-input>
				</div>
			</template>
			<el-container>
				<el-main style="padding: 0 20px 20px 20px">
					<el-form label-position="top">
						<el-form-item label="审批人员类型">
							<el-select v-model="form.setType">
								<el-option :value="EApproverType.USER" label="指定成员"></el-option>
								<el-option :value="EApproverType.DIRECTOR" label="主管"></el-option>
								<el-option :value="EApproverType.MULTI_LEVEL" label="连续多级主管"></el-option>
							</el-select>
						</el-form-item>

						<el-form-item v-if="form.setType === EApproverType.USER" label="选择成员">
							<el-button type="primary" :icon="Plus" round @click="openSelect(EUserSelectType.USER)">选择人员</el-button>
							<div class="tags-list">
								<el-tag v-for="(user, index) in form.nodeUserList" :key="user.id" closable @close="delUser(index)">{{ user.name }}</el-tag>
							</div>
						</el-form-item>

						<el-form-item v-if="form.setType === EApproverType.DIRECTOR" label="指定主管">
							发起人的第 <el-input-number v-model="form.examineLevel" :min="1" /> 级主管
						</el-form-item>

						<el-form-item v-if="form.setType === EApproverType.MULTI_LEVEL" label="连续主管审批终点">
							<el-radio-group v-model="form.directorMode">
								<el-radio :label="EDirectorMode.TOP">直到最上层主管</el-radio>
								<el-radio :label="EDirectorMode.CUSTOM">自定义审批终点</el-radio>
							</el-radio-group>
							<p v-if="form.directorMode === EDirectorMode.CUSTOM">直到发起人的第 <el-input-number v-model="form.directorLevel" :min="1" /> 级主管</p>
						</el-form-item>

						<el-divider></el-divider>
						<el-form-item label="">
							<el-checkbox v-model="form.termAuto" label="超时自动审批"></el-checkbox>
						</el-form-item>
						<template v-if="form.termAuto">
							<el-form-item label="审批期限（为 0 则不生效）">
								<el-input-number style="width: 140px; margin-right: 10px" v-model="form.term" :min="0" /> 小时
							</el-form-item>
							<el-form-item label="审批期限超时后执行">
								<el-radio-group v-model="form.termMode">
									<el-radio :label="ETermMode.PASS">自动通过</el-radio>
									<el-radio :label="ETermMode.REJECT">自动拒绝</el-radio>
								</el-radio-group>
							</el-form-item>
						</template>

						<el-divider></el-divider>
						<el-form-item label="多人审批时审批方式">
							<el-radio-group v-model="form.examineMode">
								<p style="width: 100%"><el-radio :label="EExamineMode.ORDER">按顺序依次审批</el-radio></p>
								<p style="width: 100%"><el-radio :label="EExamineMode.EVERY">会签 (可同时审批，每个人必须审批通过)</el-radio></p>
								<p style="width: 100%"><el-radio :label="EExamineMode.SOME">或签 (有一人审批通过即可)</el-radio></p>
							</el-radio-group>
						</el-form-item>
					</el-form>
				</el-main>

				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="drawerVisible = false">取消</el-button>
				</el-footer>
			</el-container>

			<UserRoleSelect ref="selectRef" v-model="form.nodeUserList" />
		</el-drawer>
	</div>
</template>
